package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.mapper.QuestionRadioMapper;
import com.ofps.forms.domain.QuestionRadio;
import com.ofps.forms.service.QuestionRadioService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionRadioService")
public class QuestionRadioServiceImpl extends ServiceImpl<QuestionRadioMapper, QuestionRadio> implements QuestionRadioService {

    @Resource
    private QuestionRadioMapper questionRadioMapper;

    @Override
    public R save(HttpServletRequest request, QuestionRadio questionRadio) {
        //userId
//        questionRadio.setUserId(TokenUtil.getId(request));
        questionRadio.setUserId(Math.toIntExact(getUserId()));
        return this.save(questionRadio) ? R.ok("保存成功") : R.error("保存失败");

    }

    @Override
    public R remove(Map<String, Object> body) {

        QueryWrapper<QuestionRadio> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return 1 == questionRadioMapper.delete(queryWrapper) ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public List<QuestionRadio> queryQuestionRadioWithId(Map<String, Object> body) {
        QueryWrapper<QuestionRadio> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionRadioMapper.selectList(queryWrapper);
    }

}
